﻿Imports System.Data
Imports System.Data.OleDb


Public Class Form2
    Public conexion As New OleDbConnection("Provider=Microsoft.ACE.Oledb.12.0;Data Source=C:\Users\Sara\Desktop\Database\Agenda.accdb")
    Public adapter As New OleDbDataAdapter("Select * from Tabla1", conexion)
    Public midataset As New DataSet

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Hide()
    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        midataset.Clear()
        adapter.Fill(midataset, "Tabla1")
        DataGridView1.DataSource = midataset
        DataGridView1.DataMember = "Tabla1"

        'Volcamos el contenido de la database en el combobox
        ComboBox1.DataSource = midataset.Tables("Tabla1")
        ComboBox1.DisplayMember = "apellidos"
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ' Dim comando As New OleDbCommand("Select * from Tabla1 where apellidos=?", conexion)
        'adapter.SelectCommand = comando
        'comando.Parameters.Add("@apellidos", OleDbType.VarChar, 15).Value = ComboBox1.Text
        'midataset.Clear()
        'adapter.Fill(midataset, "Tabla1")
        'DataGridView1.DataSource = midataset


        Dim cb As New OleDbDataAdapter
        Dim comando As New OleDbCommand("Select * from Tabla1 where apellidos=?", conexion)
        Dim ds As New DataSet
        cb.SelectCommand = comando
        comando.Parameters.Add("@apellidos", OleDbType.VarChar, 15).Value = ComboBox1.Text
        ds.Clear()
        cb.Fill(ds, "Tabla1")
        DataGridView1.DataSource = ds

        TextBox3.Text = DataGridView1.RowCount - 1
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim comando As New OleDbCommand(("Select * from Tabla1 where apellidos like '%" & TextBox1.Text & "%'"), conexion)
        adapter.SelectCommand = comando
        midataset.Clear()
        adapter.Fill(midataset, "Tabla1")
        DataGridView1.DataSource = midataset
        TextBox3.Text = DataGridView1.RowCount - 1
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        Dim cb As New OleDbDataAdapter
        Dim comando As New OleDbCommand(("Select * from Tabla1 where (apellidos like?)"), conexion)
        Dim ds As New DataSet
        cb.SelectCommand = comando
        comando.Parameters.Add("@apellidos", OleDbType.VarChar, 15).Value = TextBox2.Text
        ds.Clear()
        cb.Fill(ds, "Tabla1")
        DataGridView1.DataSource = ds
        TextBox3.Text = DataGridView1.RowCount - 1
    End Sub
End Class